/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Seguridad.Vistas;

import Seguridad.Controlador.controladorseguridad;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.swing.table.AbstractTableModel;
import modelos.Usuario;
import modelos.Rol;
import paquetevistas.Escritorio;

import javax.swing.JOptionPane;
import javax.swing.JTable;
import paquetevistas.Escritorio;

/**
 *
 * @author Saori
 */
public class RolBuscar extends javax.swing.JInternalFrame {

    /**
     * Creates new form RolBuscar
     */
    public static Usuario usuario = lbj.LBJ.Login.usuario;
    public static RolNuevo rolnuevo;
    public static RolEditar roleditar;
    RolTableModel myRolTableModel=new RolTableModel();
    public static controladorseguridad mycontroladorseguridad = new controladorseguridad();
    
    class RolTableModel extends AbstractTableModel {

        List<Rol> roles = new ArrayList<Rol>();

        public RolTableModel() {
//            roles = mycontroladorseguridad.sacaroles();
            roles = mycontroladorseguridad.buscarroles("","");
        }
        String[] titles = {"Codigo", "Rol", "Descripcion", "Estado"};

        public int getRowCount() {
            return roles.size();
        }

        public int getColumnCount() {
            return 4;
        }

        public Object getValueAt(int rowIndex, int columnIndex) {
            String res = null;
            Rol r = roles.get(rowIndex);
            switch (columnIndex) {
                case 0:
                    res = "" + r.getIdrol();
                    break;
                case 1:
                    res = r.getNombre();
                    break;
                case 2:
                    res = "" + r.getDescripcion();
                    break;
                case 3:
                    if(r.getEstado()==1) res="Activo";
                    else res="Inactivo";
                    
                    break;
            }
            return res;
        }

        public String getColumnName(int col) {
            return titles[col];
        }
    }
    
    public RolBuscar() {
        initComponents();
        tblResultadoUsuario.setModel(myRolTableModel);
        refreshRolTable();
        mycontroladorseguridad.insertarlog(usuario, new Date(), "Consultar", "Rol", "Consultar Roles");
    }
    
    public void refreshRolTable() {
//        myRolTableModel.roles = mycontroladorseguridad.sacaroles();
        myRolTableModel.roles = mycontroladorseguridad.buscarroles("","");
        myRolTableModel.fireTableChanged(null);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        btnAgregarRol = new javax.swing.JButton();
        btnBuscarRol = new javax.swing.JButton();
        btnEliminar = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblResultadoUsuario = new javax.swing.JTable();
        jPanel2 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        txtNombreRol = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        cmbEstadoRol = new javax.swing.JComboBox();

        setClosable(true);
        setIconifiable(true);
        setTitle("Mantenimiento de Roles");

        btnAgregarRol.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/file plus.png"))); // NOI18N
        btnAgregarRol.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAgregarRolActionPerformed(evt);
            }
        });

        btnBuscarRol.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/search.png"))); // NOI18N
        btnBuscarRol.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscarRolActionPerformed(evt);
            }
        });

        btnEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/trash.png"))); // NOI18N
        btnEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEliminarActionPerformed(evt);
            }
        });

        jLabel1.setText("Agregar");

        jLabel2.setText("Buscar");

        jLabel3.setText("Eliminar");

        tblResultadoUsuario.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tblResultadoUsuario.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblResultadoUsuarioMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblResultadoUsuario);

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Criterios de Búsqueda"));

        jLabel4.setText("Nombre:");

        txtNombreRol.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtNombreRolActionPerformed(evt);
            }
        });

        jLabel7.setText("Estado:");

        cmbEstadoRol.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Seleccionar", "Activo", "Inactivo" }));
        cmbEstadoRol.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbEstadoRolActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel4)
                .add(27, 27, 27)
                .add(txtNombreRol, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 100, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(70, 70, 70)
                .add(jLabel7)
                .add(18, 18, 18)
                .add(cmbEstadoRol, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(78, 78, 78))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .add(19, 19, 19)
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel4)
                    .add(txtNombreRol, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel7)
                    .add(cmbEstadoRol, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(21, 21, 21))
        );

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 486, Short.MAX_VALUE)
                    .add(jPanel1Layout.createSequentialGroup()
                        .add(11, 11, 11)
                        .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel1)
                            .add(btnAgregarRol, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 46, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jPanel1Layout.createSequentialGroup()
                                .add(21, 21, 21)
                                .add(jLabel2)
                                .add(29, 29, 29))
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(btnBuscarRol, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 46, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .add(18, 18, 18)))
                        .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jPanel1Layout.createSequentialGroup()
                                .add(4, 4, 4)
                                .add(jLabel3))
                            .add(btnEliminar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 46, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                    .add(jPanel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .add(16, 16, 16))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .add(22, 22, 22)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(btnBuscarRol, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 46, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                        .add(btnAgregarRol, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 46, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(btnEliminar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 46, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(jLabel2)
                    .add(jLabel3))
                .add(28, 28, 28)
                .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(42, 42, 42)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 89, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        jPanel2.getAccessibleContext().setAccessibleName("");

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnAgregarRolActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAgregarRolActionPerformed
        // TODO add your handling code here:
        if(rolnuevo == null || rolnuevo.isClosed()){
            rolnuevo = new RolNuevo();
            Escritorio.rol.getDesktopPane().add(rolnuevo);
            rolnuevo.setVisible(true);
        }else
            try{
                rolnuevo.setSelected(true);
            }catch(Exception e){} 

    }//GEN-LAST:event_btnAgregarRolActionPerformed

    private void cmbEstadoRolActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbEstadoRolActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cmbEstadoRolActionPerformed

    private void btnEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminarActionPerformed
        // TODO add your handling code here:
        if(tblResultadoUsuario.getSelectedRow()!=-1){
            int resp = JOptionPane.showConfirmDialog(null, "Está seguro de eliminar el rol?","Mensaje",JOptionPane.INFORMATION_MESSAGE);
            if(resp==0){
                int row=tblResultadoUsuario.getSelectedRow();
                int column=tblResultadoUsuario.getSelectedColumn();
                int eliminar=mycontroladorseguridad.eliminarRol(row,column);
                
                if(eliminar == 1)
                    mycontroladorseguridad.insertarlog(usuario, new Date(), "Eliminar", "Rol", "Eliminar Rol");
                else if(eliminar == 0)
                    JOptionPane.showMessageDialog(null, "No se pudo eliminar el Rol: Se encuentra relacionado a un Usuario");
                else
                    JOptionPane.showMessageDialog(null, "No se pudo eliminar el Rol.");
                
                //volver a cargar la tabla
                myRolTableModel.roles = mycontroladorseguridad.buscarroles("","");
                myRolTableModel.fireTableChanged(null);
            }
        }
        else
            JOptionPane.showMessageDialog(null, "No ha seleccionado ningún elemento a eliminar.","Mensaje",JOptionPane.INFORMATION_MESSAGE);
    }//GEN-LAST:event_btnEliminarActionPerformed

    private void btnBuscarRolActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarRolActionPerformed
        // TODO add your handling code here:
        myRolTableModel.roles = mycontroladorseguridad.buscarroles(txtNombreRol.getText(),cmbEstadoRol.getSelectedItem().toString());
        myRolTableModel.fireTableChanged(null);
        mycontroladorseguridad.insertarlog(usuario, new Date(), "Consultar", "Rol", "Consultar Roles");
    }//GEN-LAST:event_btnBuscarRolActionPerformed

    private void txtNombreRolActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtNombreRolActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtNombreRolActionPerformed

    private void tblResultadoUsuarioMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblResultadoUsuarioMouseClicked
        // TODO add your handling code here:
        if(evt.getClickCount()==2){
            int row=tblResultadoUsuario.getSelectedRow();
            int column=tblResultadoUsuario.getSelectedColumn();
            int codigo=Integer.parseInt(tblResultadoUsuario.getValueAt(row, 0).toString());
            Rol role = mycontroladorseguridad.getRolById(codigo);
            roleditar = new RolEditar(role);
            Escritorio.rol.getDesktopPane().add(roleditar);
            roleditar.setVisible(true);
//            mycontroladorseguridad.eliminarRol(row, column);
        }
    }//GEN-LAST:event_tblResultadoUsuarioMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAgregarRol;
    private javax.swing.JButton btnBuscarRol;
    private javax.swing.JButton btnEliminar;
    private javax.swing.JComboBox cmbEstadoRol;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblResultadoUsuario;
    private javax.swing.JTextField txtNombreRol;
    // End of variables declaration//GEN-END:variables
}
